package net.xdclass.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author : zhen.xue
 * @date : 2021-05-28 11:42
 */
public class CartVO {


    /**
     * 购物项
     */
    @Setter
    @Getter
    @JsonProperty("cart_items")
    private List<CartItemVO> cartItems;

    /**
     * 购买总件数
     */
    @JsonProperty("total_num")
    private Integer totalNum;

    /**
     * 购买总价格
     */
    @JsonProperty("total_amount")
    private BigDecimal totalAmount;

    /**
     * 购买实际支付价格
     */
    @JsonProperty("real_pay_amount")
    private BigDecimal realPayAmount;

    public Integer getTotalNum() {
        if(this.cartItems != null){
            return cartItems.stream().mapToInt(CartItemVO::getBuyNum).sum();
        }
        return 0;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal(0);
        if(this.cartItems != null){
            for (CartItemVO cartItem : cartItems) {
                BigDecimal itemAmount = cartItem.getTotalAmount();
                amount = amount.add(itemAmount);
            }
        }
        return amount;
    }

    public BigDecimal getRealPayAmount() {
        BigDecimal amount = new BigDecimal(0);
        if(this.cartItems != null){
            for (CartItemVO cartItem : cartItems) {
                BigDecimal itemAmount = cartItem.getTotalAmount();
                amount = amount.add(itemAmount);
            }
        }
        return amount;
    }
}